package org.richin.io.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

public class Test {
	/**

	 * 将指定的文本内容写入到指定路径的文件

	 * @param path 目标文件路径

	 * @param content 需要写入的内容

	 * @return 写入结果

	 */

	public static boolean writeToFile(String path, String content)

	{

		final int CACHE = 1024;

		boolean result = false;

		FileOutputStream outFile = null;

		FileChannel channel = null;

		// 将字符串转换为字节数组

		final byte[] bt = content.getBytes();

		ByteBuffer buf = null;

		try

		{

			outFile = new FileOutputStream(path);

			channel = outFile.getChannel();

			// 以指定的缓存分隔字节数组，得到缓存次数

			int size = bt.length / CACHE;

			// 得到被缓存分隔后剩余的字节个数

			int mod = bt.length % CACHE;

			int start = 0;

			int end = 0;

			for (int i = 0; i < size + 1; i++)

			{

				if (i == size)

				{

					if (mod > 0)

					{

						// 分配新的字节缓冲区

						buf = ByteBuffer.allocate(mod);

						start = end;

						end += mod;

					}

					else

					{

						break;

					}

				}

				else

				{

					// 分配新的字节缓冲区

					buf = ByteBuffer.allocate(CACHE);

					start = end;

					end = (i + 1) * CACHE;

				}

				// 以指定的始末位置获取一个缓存大小的字节

				byte[] bytes = getSubBytes(bt, start, end);

				for (int j = 0; j < bytes.length; j++)

				{

					buf.put(bytes[j]);

				}

				// 反转缓冲区，为通道写入做好准备

				buf.flip();

				// 利用通道写入文件

				channel.write(buf);

				result = true;

			}

		}

		catch (IOException e)

		{

			e.printStackTrace();

		}

		finally

		{

			// 关闭所有打开的IO流

			try

			{

				channel.close();

				outFile.close();

			}

			catch (IOException e)

			{

				e.printStackTrace();

			}

		}

		return result;

	}

	/**

	 * 以指定的始末位置从字节数组中获取其子数组

	 * @param bt 原始字节数组

	 * @param start 起始位置

	 * @param end 结束位置

	 * @return 子字节数组

	 */

	private static byte[] getSubBytes(byte[] bt, int start, int end)

	{

		int size = end - start;

		byte[] result = new byte[size];

		for (int i = 0; i < size; i++)

		{

			result[i] = bt[i + start];

		}

		return result;

	}

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main1(String[] args) throws IOException {
		//File file = new File("c:/add.txt");
		String filename = "c:/add.txt";
		FileInputStream fis = new FileInputStream(filename);
		FileChannel channel = fis.getChannel();
		int length = (int) channel.size();
		MappedByteBuffer byteBuffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, length);
		Charset charset = Charset.forName("GBK");
		CharsetDecoder decoder = charset.newDecoder();
		CharBuffer charBuffer = decoder.decode(byteBuffer);

		for (int i = 0, n = charBuffer.length(); i < n; i++) {
			System.out.print(charBuffer.get());
		}
	}

	static int length = 0x8FFFFFF; //  128  Mb     


	public static void mainr(String[] args) throws IOException {
		long t1 = System.currentTimeMillis();

		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("c:/test.dat")));
		char[] data = new char[8 * 1024];
		int n = 0;
		while ((n = br.read(data)) != -1) {
		}
		br.close();
		System.out.println("BufferedReader:" + (System.currentTimeMillis() - t1) + "毫秒");

		t1 = System.currentTimeMillis();
		RandomAccessFile raf = new RandomAccessFile("c:/test.dat", "r");
		byte[] b1 = new byte[8 * 1024];
		n = 0;
		while ((n = raf.read(b1)) != -1) {
		}
		raf.close();
		System.out.println("RandomAccessFile:" + (System.currentTimeMillis() - t1) + "毫秒");

	}
}
